﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using Omu.Awesome.Core;
using XPressify.Core;
using XPressify.Data.Helpers;

namespace XPressify.Data.CompliedQueries
{
    public class MediaCompiledQueries
    {


        public static Func<Repository, Guid, Media> GetCoverOf
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Media>(
                    (context, albumId) => context.BaseObjects.OfType<Media>().SingleOrDefault(cov => cov.Album.Id == albumId && cov.IsCover)
                    );
            }
        }

        public static Func<Repository, Guid, Media> GetProfileOf
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Media>(
                    (context, albumId) => context.BaseObjects.OfType<Media>().SingleOrDefault(cov => cov.Album.Id == albumId && cov.IsProfile)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Media>> GetMediasOf
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable< Media>>(
                    (context, albumId) => context.BaseObjects.OfType<Media>().Where(med => med.Album.Id == albumId).OrderBy(med => med.Created)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Media>> GetMediasOfDesc
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Media>>(
                    (context, albumId) => context.BaseObjects.OfType<Media>().Where(med => med.Album.Id == albumId).OrderByDescending(med => med.Created)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Media>> GetPhotoMediasOf
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Media>>(
                    (context, albumId) => context.BaseObjects.OfType<Media>().Where(med => med.Album.Id == albumId && med.MediaType == MediaType.Photo).OrderBy(x => x.Created)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Media>> GetPhotoMediasOfDesc
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Media>>(
                    (context, albumId) => context.BaseObjects.OfType<Media>().Where(med => med.Album.Id == albumId && med.MediaType == MediaType.Photo).OrderByDescending(x => x.Created)
                    );
            }
        }

        public static Func<Repository, Guid, Media> GetMedia
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Media>(
                    (context, mediaId) => context.BaseObjects.OfType<Media>().SingleOrDefault(med => med.Id == mediaId)
                    );
            }
        }

        public static Func<Repository, Guid, BaseSubject> GetMediaSubject
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, BaseSubject>(
                    (context, mediaId) => context.BaseSubjects.FirstOrDefault(usr => usr.BaseObjects.Select(sub => sub.Id).Contains(mediaId))
                    );
            }
        }

    }
}